#!/usr/bin/env bash
# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: chensili

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(git rev-parse --show-toplevel)"
LOG_DIR="${REPO_ROOT}/../log"

DATETIME="$(date +%Y%m%d-%H%M%S%z)"
LOG_FILE="log_download_s3.${DATETIME}"

mkdir -p "${LOG_DIR}"
cd "${REPO_ROOT}"

source "${SCRIPT_DIR}/py_bootstrap.sh"

MACHINES=(
  "feed-08.ap-northeast-1.aws"
)

if [ -n "${1}" ]; then
  TARGET_DATE="${1}"
else
  TARGET_DATE="$(date --date='1 day ago' '+%Y%m%d')"
fi

BUCKET="luke.corp.prestolabs.io"

# https://docs.aws.amazon.com/cli/latest/topic/s3-config.html
aws configure set default.s3.max_bandwidth 10MB/s

# https://github.com/aws/aws-cli/pull/2747
OPTIONS="--no-progress"

echo "BUCKET='${BUCKET}'"
date

for machine in ${MACHINES[*]}
do
  SRC_DIR="s3://${BUCKET}/data/flow/${machine}/${TARGET_DATE}"
  DST_DIR="/remote/iosg/coin/data/flow/${machine}/${TARGET_DATE}"
  echo ${SRC_DIR} ${DST_DIR}
  aws s3 sync ${OPTIONS} "${SRC_DIR}" "${DST_DIR}" \
      2>> "${LOG_DIR}/${LOG_FILE}"
done

date
